<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="学生">
          <check-stu :itemId="searchForm.stuid" @checkedItem="checkedStu"/>
        </el-form-item>
        <el-form-item label="老师">
          <check-emp :itemId="searchForm.empid" @checkedItem="checkedEmp"/>
        </el-form-item>
        <!-- <el-form-item label="时间范围">
          <el-date-picker v-model="dateTime" type="daterange" range-separator="~" start-placeholder="开始日期"
            end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="sing(null)">批量签到</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }" @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>
      <el-table-column prop="" label="签到" width="120">
        <template slot-scope="scop">
          <el-button type="primary" plain size="mini" @click="sing(scop.row.paymentid)">签到</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="stuname" label="学员姓名"></el-table-column>
      <el-table-column prop="stuphone" label="手机号"></el-table-column>
      <el-table-column prop="lessioninfo" label="课程信息"></el-table-column>
      <el-table-column prop="empname" label="任课老师"></el-table-column>
      <el-table-column prop="alltimes" label="已上/总课次">
        <template slot-scope="scop">
          {{scop.row.usedtimes + ' / ' + scop.row.alltimes}}
          <el-progress :text-inside="true" :stroke-width="16" :percentage="((scop.row.usedtimes*100) / scop.row.alltimes).toFixed(2)*1"></el-progress>
        </template>
      </el-table-column>
      
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="签到" center :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="singForm" ref="ruleForm" label-width="100px" class="edit-form">
        <el-form-item label="已选择学员：" v-if="paymentids.length>0">
          <el-tag type="success" v-for="e in paymentids" :key="e">{{tableData.filter(m=>m.paymentid === e)[0].stuname}}</el-tag>
        </el-form-item>
        <el-form-item label="签到类型：" v-if="paymentids.length>0">
          <el-radio-group v-model="singForm.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="2">请假</el-radio>
            <el-radio :label="3">补课</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上课内容：" prop="empid">
          <el-input type="textarea" :rows="2" placeholder="请输入上课内容" v-model="singForm.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="doSign">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findSign, dosign } from "@/api/user";
import checkStu from "@/views/components/checkStu";
import checkEmp from "@/views/components/checkEmp";
export default {
  components:{
    checkStu,
    checkEmp
  },
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      checkItemIds: [],
      paymentids: [],
      dialogFormVisible: false,
      dateTime:'',
      searchForm: {
        // begintime: '',
        // endtime: '',
        stuid: '',
        empid: '',
      },
      singForm: {
        content: '',
        status: 1
      }
    }
  },
  computed: {
    queryData() {
      return {
        stuid: this.searchForm.stuid,
        empid: this.searchForm.empid,
        // begintime: this.searchForm.begintime,
        // endtime: this.searchForm.endtime,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  watch: {
    dateTime(n) {
      if (n) {
        this.searchForm.begintime = n[0]
        this.searchForm.endtime = n[1]
      } else {
        this.searchForm.begintime = ''
        this.searchForm.endtime = ''
      }
    }
  },
  methods: {
    async getData() {
      const res = await findSign.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.signlist;
        this.total = res.data.count;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.checkItemIds = val.map(e => e.paymentid)
      }
    },
    checkedStu(val){
      this.searchForm.stuid = val
    },
    checkedEmp(val){
      this.searchForm.empid = val
    },
    sing(ids) {
      this.singForm.content = ''
      this.paymentids = []
      if (ids) {
        this.paymentids = [ids]
      } else {
        if (this.checkItemIds.length < 1) {
          this.$message.error('请先选择学员！')
          return
        } else {
          this.paymentids = this.checkItemIds
        }
      }
      this.dialogFormVisible = true
    },
    async doSign() {
      const res = await dosign.post({ paymentids: this.paymentids, content: this.singForm.content, status:this.singForm.status });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.dialogFormVisible = false
        this.paymentids = []
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
